package com.example.sentineldemo.common;

/**
 * Created with IntelliJ IDEA 2023.
 * Description:
 * User: 故我
 * Date: 2024-02-15
 * Time: 21:52
 */

import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Objects;

/**
 * 定义一个系统的异常 用于处理热点参数
 */
@RestControllerAdvice   // responseBody  + ControllerAdvice
public class SystemException {
    @ExceptionHandler(ParamFlowException.class) // 处理哪个异常
    public HashMap<String, Object> paramFlowException(ParamFlowException e) {
        return new HashMap<>() {{
            put("code", HttpStatus.TOO_MANY_REQUESTS.value());
            put("msg", "请求被热点参数限流了!");
        }};

    }
}
